//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@infratel.com), 2002-2004
//
// Set direction attribute for object.
//

# include "StdAfx.h"
# include "SetDirIndicator.h"
# include "UndoRedo.h"
# include "Label.h"

class CSetDirIndicatorAction : public CEditAction {
  public:
	CSetDirIndicatorAction (rgn_t & _rgn, bool _bDirIndicator) :
		m_rgn (_rgn),
		m_bNewDirIndicator (_bDirIndicator),
		m_bOldDirIndicator ((_rgn.dwFlags & rgn_t::fDirIndicator) != 0),
		m_bWasModified ((_rgn.dwFlags & rgn_t::fModified) != 0)
	{}

	virtual void GetName (string_t & _str) const;
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo () {CSetDirIndicatorAction::Do ();}

  private:
	rgn_t &    m_rgn;
	bool const m_bNewDirIndicator;
	bool const m_bOldDirIndicator;
	bool const m_bWasModified;
};

void CSetDirIndicatorAction::GetName (string_t & _str) const {
	CCritSec::CLock lock (g_cs);

	_str.append ("set direction for [");
	GetHintText (m_rgn, _str);
	_str.append ("]");
}

bool CSetDirIndicatorAction::Do () {
	CCritSec::CLock lock (g_cs);

	if (m_bNewDirIndicator)
		m_rgn.dwFlags |= rgn_t::fDirIndicator;
	else
		m_rgn.dwFlags &= ~rgn_t::fDirIndicator;

	m_rgn.dwFlags |= rgn_t::fModified;

	return true;
}

void CSetDirIndicatorAction::Undo () {
	CCritSec::CLock lock (g_cs);

	if (m_bOldDirIndicator)
		m_rgn.dwFlags |= rgn_t::fDirIndicator;
	else
		m_rgn.dwFlags &= ~rgn_t::fDirIndicator;

	if (! m_bWasModified)
		m_rgn.dwFlags &= ~rgn_t::fModified;
}

///////////////////////////

void SetDirIndicator (rgn_t & _rgn, bool _bDirIndicator) {
	PerformEditAction (new CSetDirIndicatorAction (_rgn, _bDirIndicator));
}
